﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdDrawProfileBase.h"

#include "CmdStatusUtils.h"
#include "PropertyWidgetUtils.h"
#include "StructureFamilyUtils.h"
#include "GbmpPresetParameterUIUtils.h"
#include "StructureInstancePresetParameterUtils.h"

#include "UniIdentity.h"
#include "IDocument.h"
#include "IFamily.h"
#include "IInstanceType.h"
#include "IUserTransaction.h"
#include "IElementBasicInformation.h"
#include "IPropertyPanelEventArgs.h"
#include "IEditMode.h"
#include "IStructureEditModeBehavior.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

bool CmdDrawProfileBase::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

bool CmdDrawProfileBase::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

void CmdDrawProfileBase::OnPropertyPanelEvent(IPropertyPanelEventArgs * pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    // TODO: 接口
    if (PropertyWidgetUtils::IsComponentTypeChangedEvent(pArgs->GetPropertyItemUniqueId()))
    {
        ElementId newInstanceTypeId(StringUtil::ToNumber<Int64>(pArgs->GetNewValue()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(newInstanceTypeId.IsValid(), L"newInstanceTypeId无效",L"GDMPLab",L"2024-03-30");

        ElementId newFamilyId;
        bool bOk = StructureFamilyUtils::GetFamilyIdByTypeId(pDoc, newInstanceTypeId, newFamilyId);
        DBG_WARN_AND_RETURN_VOID_UNLESS(bOk, L"GetFamilyNameByTypeId失败",L"GDMPLab",L"2024-03-30");

        if (m_familyId.HasSameValue(newFamilyId) && m_instanceTypeId.HasSameValue(newInstanceTypeId))
            return;

        this->SetFamilyIdAndInstanceTypeId(newFamilyId, newInstanceTypeId);
    }
}

void CmdDrawProfileBase::OnEnterGettingEndPointState(IUiView * pCurrentView)
{
    // 需要确认：在绘制轮廓线和坡度线的过程中是否需禁用预设参数的属性面板？
    //GbmpPresetParameterUIUtils::EnablePropertyPanel(false);
}

void CmdDrawProfileBase::OnActionDestroy()
{
    GbmpPresetParameterUIUtils::UpdatePropertyPanel(ElementId::InvalidID);
    GbmpPresetParameterUIUtils::EnablePropertyPanel(true);
}

bool CmdDrawProfileBase::SetFamilyIdAndInstanceTypeId(ElementId familyId, ElementId instanceTypeId)
{
    if (m_familyId.HasSameValue(familyId) && m_instanceTypeId.HasSameValue(instanceTypeId))
        return true;

    m_familyId = familyId;
    m_instanceTypeId = instanceTypeId;

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_familyId.IsValid(), L"m_familyId无效",L"GDMPLab",L"2024-03-30");
    const IFamily* pFamily = quick_cast<const IFamily>(pDoc->GetElement(m_familyId));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFamily, L"pFamily为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"保存当前创建实例的族类型到预设参数"), true);

    const IElementBasicInformation* pBasicInfo = pFamily->GetBasicInformation();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pBasicInfo, L"pBasicInfo为空",L"GDMPLab",L"2024-03-30");
    UniIdentity uid = pBasicInfo->GetCategoryUid();

    int secondKey = 0;
    bool bOk = StructureInstancePresetParameterUtils::SetPresetInstanceType(pDoc, uid, secondKey, m_instanceTypeId);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"SetPresetInstanceType失败",L"GDMPLab",L"2024-03-30");

    IEditMode* pProfileEditMode = IEditMode::GetTopActiveEditMode(pDoc);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pProfileEditMode, L"获取当前编辑模式失败！",L"GDMPLab",L"2024-03-30");
    IStructureEditModeBehavior* pProfileEDBehavior = quick_cast<IStructureEditModeBehavior>(pProfileEditMode->GetEditModeBehavior());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pProfileEDBehavior, L"pProfileEDBehavior为空！",L"GDMPLab",L"2024-03-30");
    pProfileEDBehavior->SetEditingFamilyId(m_familyId);
    pProfileEDBehavior->SetEditingInstanceTypeId(m_instanceTypeId);

    opTransaction->Commit();

    return true;
}
